<?php

namespace App\Http\Controllers\Api;

use App\Blocks\User;
use App\Models\ActivityGoods;
use App\Http\Controllers\Controller;
use App\Libs\Message;
use App\Models\ActivityPrizes;
use App\Models\ActivityUser;
use App\Models\WechatOpenplatformToken;
use EasyWeChat\Factory;
use Illuminate\Http\Request;

class ActivityGoodsController extends Controller
{
    public function goodsList(){
        $goodsList = ActivityGoods::all();

        return response(Message::setResponseInfo('SUCCESS', $goodsList));
    }

    public function choseGoods(Request $request){
        $goodsId = $request->get('goods_id');
        if(empty($goodsId)){
            return response()->json(Message::setResponseInfo('PARAMETER_ERROR'));
        }

        global $g_uid;

        $userBlock = new User();
        $status = $userBlock->choseGoods($g_uid, $goodsId);
        if($status === false){
            return response()->json(Message::setResponseInfo('FAILED'));
        }elseif ($status === -1){
            return response()->json(Message::setResponseInfo('REPEAT_SUBMIT'));
        }else{
            return response()->json(Message::setResponseInfo('SUCCESS'));
        }
    }

    public function answer(Request $request){
        $askuId = $request->get('ask_uid');
        $answer = $request->get('answer');
        if(empty($askuId)){
            return response()->json(Message::setResponseInfo('PARAMETER_ERROR'));
        }

        if(empty($answer)){
            return response()->json(Message::setResponseInfo('PARAMETER_ERROR'));
        }

        global $g_uid;



        $userBlock = new User();
        $status = $userBlock->answer($g_uid,$askuId, $answer);

        if($status >= 0){
            return response()->json(Message::setResponseInfo('SUCCESS', ['result' => $status]));
        }elseif($status === false){
            return response()->json(Message::setResponseInfo('FAILED'));
        }elseif ($status === -1){
            return response()->json(Message::setResponseInfo('REPEAT_SUBMIT'));
        } elseif ($status === -2){
            return response()->json(Message::setResponseInfo('IS_YOURSELF'));
        }

    }

    public function prize(Request $request){
        global $g_uid;
        $userBlock = new User();

        $userInfo = $userBlock->getActivityUserInfoByUid($g_uid);

        $userInfo->xiaohongbao = (int)(($userInfo->popularity/6) + ($userInfo->tacit / 10));
        $userInfo->dahongbao = (int)($userInfo->tacit / 20);

        $cycount = \Cache::get('cy:count');
        if(empty($count)){
            $cycount = ActivityUser::whereNotNull('answer_goods_id')->count();
        }

        $type = $request->get('type');
        if($type==1) {
            return view('prize-xingguangbaohe', ['cyCount'=> $cycount, 'wid' => $userInfo->appid,'user' => $userInfo, 'token' => $request->get('token'), 'type' => $type]);
        }elseif($type==2){
            return view('prize-yueguangbaohe', ['cyCount'=> $cycount,'wid' => $userInfo->appid,'user' => $userInfo, 'token' => $request->get('token'), 'type' => $type]);
        }
    }

    public function prizeLog(Request $request){
        $type = $request->get('type');
        if(empty($type)){
            return response()->json(Message::setResponseInfo('PARAMETER_ERROR'));
        }

        global $g_uid;
        $userBlock = new User();

        $prizeId = $userBlock->prizeLog($type, $g_uid);

        if($prizeId < 0){
            return response()->json(Message::setResponseInfo('', '', '5032', '抽奖次数不够'));
        }elseif($prizeId > 0){
            $userBlock = new User();

            $userInfo = $userBlock->getActivityUserInfoByUid($g_uid);
            $wechat = WechatOpenplatformToken::where('appid', $userInfo->appid)->first();
            if(!$wechat){
                return response('登录失败');
            }
            $config = \Config::get('wechat.open_platform.default');
            $openPlatform = Factory::openPlatform($config);
            $wechatApp = $openPlatform->officialAccount($wechat->appid, $wechat->refresh_token);

            $prize = ActivityPrizes::where('id', $prizeId)->first();

            if($prizeId == 11) {
                $remark = "请根据页面提示完整填写中奖礼品寄送信息，收货地址仅限广西区内，将在活动结束后15个工作日内为您免费寄出，如您在9月30日前仍未提交收件信息的，将视为放弃奖品。";
            }elseif(in_array($prizeId, [8,9,10,15])){
                $remark = "代金券将于活动结束后的48小时内自动充值入您参与活动的号码账户，如号码未开通翼支付账户，请在中奖后的24小时内开通，否则将充值失败视为放弃奖品。";
            }elseif(in_array($prizeId, [12,13,14])){
                $remark = "请参与活动的机主本人于2018年10月8日-2018年10月12日9:00-12:00、15:00-18:00持有效身份证件至广西南宁市青秀区民主路35号电信大楼710室客服组领取，逾期将视为放弃奖品，联系电话4008771189。";
            }
            $wechatApp->template_message->send([
                'touser' => $userInfo->openid,
                'template_id' => '5gzzC-5q0OxlS5Y0fSpQbRpWtMIvVvtBo2iuog5T8bQ',
                'url' => env('APP_URL')."/api/wechat/open-platform/login/wx7d9a866f2ffa9f1a",
                'data' => [
                    'first' => '恭喜您在中秋活动中奖了！',
                    'keyword1' => '五仁or蛋莲，你是否知我心意？',
                    'keyword2' => $prize->title,
                    'remark' => $remark
                ],
            ]);

            return response()->json(Message::setResponseInfo('SUCCESS', $prizeId));
        }else{
            return response()->json(Message::setResponseInfo('SUCCESS', 0));
        }
    }
}
